wtfaq = function() {
var myGeo = {};
var map = {};
var marker = {};
var circle = {};
var wtfaqs = [];
var questions = [];
var pad = '/wp-content/plugins/wtfaqform/';
var LOADER = '<img id=loader src="'+ pad +'ajax-loader.gif">';
var markerMoveListener = {};
var RADIUS = 200;

var cleanUp = function(t) {
		var t = t ? t : 10000;
		setTimeout(function() {
			$('#loader').remove();
			$('#message').remove();
		},t);
}

var putLoader = function(t) {
		t.after(LOADER);
}

var callbackUser_0 = function(data) {
	if (data.status == 'ok') {
		if (data.msg == 'new')
				var m = 'Nieuwe gebruiker, verzin nog een wachtwoord..';
		else
				var m = 'Bestaande gebruiker, vul je wachtwoord in...';
				
		$('input[name=email]').after('&nbsp;<span id=message>'+ m +'</span>');
	}
	cleanUp(2000);
}

var callbackUser_1 = function(data) {
	if (data.status == 'ok') {
		$('#loginblock').html($('input[name=email]').val() + '&nbsp;<a href="?logout">log uit</a>');
		$('#fs-login').fadeOut('slow');		
		cleanUp(2000);
		wtfaqs = data.wtfaqs;
		if (data.wtfaqs.length >0) {
				var selStr = "<div><label>FAQ</label><select id=selwtfaq name=selwtfaq><option value=0>Creeer nieuwe FAQ</option>";
				for (i in data.wtfaqs)  {
					selStr += "<option value="+data.wtfaqs[i].id+">"+data.wtfaqs[i].title+"</option>";
				}
				selStr += "</select></div>";
				$('#div-title').before(selStr);
				$('#selwtfaq').bind('change', callbackSelwtfaq);
		}
	}
	else {
		$('input[name=email]').focus();
		$('input[name=email]').after('&nbsp;<span id=message>'+ data.msg +'</span>');		
		cleanUp(5000);
	}
}

function callbackSelwtfaq() {
		if (this.selectedIndex > 0) {
			$('input[name=wtfaq_id]').val($(this).val());
			var ix = this.selectedIndex-1;
			$('input[name=title]').val(wtfaqs[ix].title);
			$('#frmquestion > legend').text('FAQ: ' + wtfaqs[ix].title);
			$('input[name=location]').val(wtfaqs[ix].location);
		}
		else {
			$('input[name=wtfaq_id]').val('0');
			$('input[name=title]').val('');
			$('#frmquestion > legend').text('FAQ');
			$('input[name=location]').val('');
		}
}

var sendForm = function() {
	putLoader($(this));
	$.ajax({url: pad + 'wtfaq.php',
			data: $('#formfaq').serialize(),
			dataType: 'json',
			success: callbackSendForm,
			error: cleanUp
	});	
}

var sendFormQuestion = function() {
	putLoader($(this));			
	$.ajax({url: pad + 'wtfaq.php',
			data: $('#frmquestion').serialize(),
			dataType: 'json',
			success: callbackQuestion,
			error: cleanUp
	});	
}

var callbackQuestion = function(data) {
	cleanUp(2000);
	$('#question-div').slideUp('slow');
	$('input[name=question]').val('');
	$('textarea[name=answer]').val('');
	$('#question-div').slideDown('slow');
	$('#divQuestions').remove();
	console.log(data);
	questions = data.questions;
	if (questions.length >0) {
			var selStr = "<div id='divQuestions'><label>Questions</label><select id=selquestion name=selquestion><option value=0>Creeer nieuwe vraag</option>";
			for (i in questions)  {
				selStr += "<option value="+questions[i].id+">"+ questions[i].question+"</option>";
			}
			selStr += "</select></div>";
			$('#divvraag').before(selStr);
			$('#selquestion').bind('change', callbackSelquestion);
	}
}

var callbackSendForm = function(data) {
	cleanUp(2000);
	if (data.status == 'ok') {
		$('#formfaq-div').slideUp('slow');
		$('#question-div').slideDown('slow');
		google.maps.event.removeListener(markerMoveListener);
		questions = data.questions;
		if (questions.length >0) {
				var selStr = "<div id='divQuestions'><label>Questions</label><select id=selquestion name=selquestion><option value=0>Creeer nieuwe vraag</option>";
				for (i in questions)  {
					selStr += "<option value="+questions[i].id+">"+ questions[i].question+"</option>";
				}
				selStr += "</select></div>";
				$('#divvraag').before(selStr);
				$('#selquestion').bind('change', callbackSelquestion);
		}

	}
	else {
			$('input[name=title]').focus();
			$('input[name=email]').after('&nbsp;<span id=message>Niet ingelogd/geen naam/geen locatie ingevuld</span>');		
			cleanUp(5000);
	}
}

var callbackSelquestion = function() {
		if (this.selectedIndex > 0) {
			var ix = this.selectedIndex-1;		
			$('input[name=question]').val(questions[ix].question);
			$('input[name=questionId]').val(questions[ix].id);
			$('textarea[name=answer]').val(questions[ix].answer);
		}
		else {
			$('input[name=question]').val('');
			$('input[name=questionId]').val('0');
			$('textarea[name=answer]').val('');
		}
}

var moveMarker = function(event) {
	/*
	var marker = new google.maps.Marker({
		      position: event.latLng,
		      map: map,
              animation: google.maps.Animation.DROP,
	});		
	*/
	marker.setPosition(event.latLng);
	map.setCenter(event.latLng);
	myGeo.geocode({ latLng: event.latLng }, zegHetVoort);
	$('input[name=geocoords]').val(JSON.stringify(event.latLng));
}

var zegHetVoort = function(results, status) {

	var i =0, j = 0;
	var name = '', del = '';;
	if (results.length > 0)
			while (i<1) {
				while (j < results[i].address_components.length) {
					name += del + results[i].address_components[j].short_name ;
					del = ', ';
					j++;
				}
				i++;
			}			
	$('input[name=location]').val(name);
}

var tellLocation = function(position) {
		var latitude = position.coords.latitude;
		var longitude = position.coords.longitude;
		
		myGeo = new google.maps.Geocoder();
		myGeo.geocode({latLng: new google.maps.LatLng(latitude,longitude)}, zegHetVoort);
		$('input[name=geocoords]').val(JSON.stringify(new google.maps.LatLng(latitude,longitude)));
		//alert(JSON.stringify(new google.maps.LatLng(latitude,longitude)));

		map = new google.maps.Map(document.getElementById("single-post-map"), {
				 zoom: 17,
				 center: new google.maps.LatLng(latitude, longitude),
				 mapTypeId: google.maps.MapTypeId.ROADMAP
			       });
		var infowindow = new google.maps.InfoWindow();			   
		var i;
		
		marker = new google.maps.Marker({
		      position: new google.maps.LatLng(latitude, longitude),
		      map: map,
              animation: google.maps.Animation.DROP,
		    });

		circle = new google.maps.Circle({
			map: map,
			radius: RADIUS
		});
		
		circle.bindTo('center', marker, 'position');
		markerMoveListener = google.maps.event.addListener(map, "click", moveMarker);
}
return {
	myGeo:myGeo
	,map:map
	,marker:marker
	,circle:circle
	,pad:pad
	,LOADER:LOADER
	,markerMoveListener:markerMoveListener
	,RADIUS:RADIUS
	,cleanUp:cleanUp
	,putLoader:putLoader
	,callbackUser_0:callbackUser_0	
	,callbackUser_1:callbackUser_1
	,sendForm:sendForm
	,sendFormQuestion:sendFormQuestion
	,callbackQuestion:callbackQuestion
	,tellLocation:tellLocation
	};
}();